


function Level.Loaded()

	local p = GetPlayer();
	
	if( GetPreviousMap() == "map1" or
		GetPreviousMap() == "" ) then
	
		p:SetPos( 160, 165 );
		p:SetCurrentSprite( 2 );

		if( not p:IsFlipped() ) then
			p:Flip();
		end
		
	end	

	Level.BackFromIntroBattle = false;
	Level.FadeFromIntroBattle = 255;
	
	Engine.BindGameCameraToEntity( p );

	Weather.GenerateFullSnow();
	
	local snoit1 = CreateNPCFromData( "Snoit", 655, 768 );
	snoit1:Flip();
	snoit1:SetDoLogic( false );

	local snoit2 = CreateNPCFromData( "Snoit", 695, 728 );
	snoit2:SetDoLogic( false );
	
	local snoit3 = CreateNPCFromData( "Snoit", 705, 778 );
	snoit3:SetDoLogic( false );
	
	AddMapEvent( 250, 300, 450, 160, function() 
		p:SetCurrentSprite( 5 );
		SetGameRule( "CanMove", 0 );
		timer.Simple( 500, function() LuaC.MessageBox2( "So... cold..", p, 40, 50, 3000, 120 ); end );
		timer.Simple( 2000, function() SetGameRule( "CanMove", 1 ); end );
	end );
	
	AddMapEvent( 250, 580, 450, 160, function() 
	
		Level.DoSnoitScene( p, snoit1, snoit2, snoit3 );
		
	end );
	
	Scene.Clear();
	
end

function Level.Think()
	
	
	
end

function Level.PostDraw()
	
	Weather.DoSnow();
	
	if( Level.BackFromIntroBattle ) then
		
		if( not Level.SorinDownSprite ) then
			
			Level.FadeOutAt = GetTicks() + 14000;
			
			GetPlayer():SetPos( 370, 530 );
			GetPlayer():SetCurrentSprite( 6 );
			GetPlayer():Scale( 1.15, 1.15 );
			GetPlayer():SetHasShadow( false );
			
			Level.SorinDownSprite = true;
			
			local npc = CreateNPC( 445, 555, { "Graphics/Sprites/Stetson/idle.spr","Graphics/Sprites/Stetson/walk.spr" } ); 
			npc:SetCurrentSprite( 0 );
			npc:SetDoLogic( false );
			npc:Scale( 2, 2 );
			npc:AddShadow( .9, .12, 0, -6 );
			
			timer.Simple( 11000, function()
			
				 npc:Flip();
				 LuaC.MessageBox( "I left him over here!", 800, 620, nil, 2600, 60, false );
			
				 timer.Simple( 2000, function()
				 	
				 	timer.Simple( 1000, function() npc:Flip() end );
					timer.Simple( 1300, function()
					
						npc:SetCurrentSprite( 1 );
						npc:SetConstantVelocity( -20, 0 );
					
		             end );
					
				 	LuaC.MessageBox( "I hope he tastes like a pretzel..", 830, 635, nil, 1500, 20, false );
				 
				 end );
				 
			end );
			
		end
		
		Draw.SetColor255( 0, 0, 0, Level.FadeFromIntroBattle );
		Draw.Rect( 0, 0, 800, 600 );
		
		if( GetTicks() < Level.FadeOutAt ) then
			
			Level.FadeFromIntroBattle = Level.FadeFromIntroBattle - 50 * GetDPS();
		
		else
			
			Level.FadeFromIntroBattle = Level.FadeFromIntroBattle + 90 * GetDPS();
			
		end
		if( Level.FadeFromIntroBattle < 0 ) then
			
			Level.FadeFromIntroBattle = 0;
			
		end
		
		Draw.SetColor255( 0, 0, 0, 255 );
		Draw.Rect( 0, 0, 800, 120 );
		Draw.Rect( 0, 480, 800, 120 );
		
		DrawFreeText();
		
	end
	
end

function Level.DoSnoitScene( p, snoit1, snoit2, snoit3 )
	
	
		SetGameRule( "CanMove", 0 );
		
		timer.Simple( 600, function() p:Face( 700, 750, true ); end );
		timer.Simple( 1000, function() snoit1:Flip();  LuaC.MessageBox2( "What's this..?  Gentle beasts of the wild..", p, 40, 50, 5000, 80 ); end );
		timer.Simple( 7500, function() LuaC.MessageBox2( "Let's kill and eat that guy.", snoit2, 40, 20, 2000, 30 );  end );
		timer.Simple( 9500, function() 

			local pos = p:GetPos();
			
			snoit1:ProjectAt( pos.x, pos.y, 400 );
			snoit2:ProjectAt( pos.x, pos.y, 400 );
			snoit3:ProjectAt( pos.x, pos.y, 400 );
			
		end );
		
		timer.Simple( 9500, function() LuaC.MessageBox2( "..What the fuc", p, 40, 50, 5000, 29 ); end );
		
		local startEscapeAttack = 0;
		local enemyEscape = false;
		local enemyDisappeared = false;
		local startBattleFade = -1;
		local battleRed = 255;
		local beginBattleDialogue = false;
		local gameTitleUnderline = 0;
		
		timer.Simple( 10000, function()
		
			LuaC.ClearMsgBoxes();
			
			StartBattle( { "Sorin" }, { "Snoit", "Snoit", "Snoit" }, function()
		
				Scene.Once( "BeginHint", function()
				
					Engine.LockBattle();
					
					LuaC.MessageBox2( "Hey! ..I'm not on the menu!", Engine.GetBattlePlayer( 0 ), 20, 20, 2600, 40 );
					
					timer.Simple( 2000, function() LuaC.TimedUIMessage2( "Press W, S to navigate the menu. D to select.", 400, 200, 5000 ); end );
					
					timer.Simple( 3000, Engine.UnlockBattle );
				
					Scene.End( "BeginHint" );
					
					end
					
				);
			
				if( #BattleEnts == 2 ) then
			
					for k, v in pairs( BattleEnts ) do
						
						if( v.isEnemy ) then
							
							Scene.Once( "StartJump", function()

								Engine.LockBattle();
								LuaC.MessageBox2( "Forget this.  You don't look tasty anyway!", v, 20, 20, 4000, 60 );
								enemyEscape = true;
								startEscapeAttack = GetTicks() + 5000;
								
								timer.Simple( 4000, function() LuaC.BattleText( "Snoit jumps high in the air!", 3000, 50 ); end );
								timer.Simple( 3800, function() v:SetHasShadow( false ); v:SetConstantVelocity( 300, -1600 ); end );
								timer.Simple( 10000, function() LuaC.BattleText( " .. and comes crashing down!", 2500, 10 ); end );
								timer.Simple( 9800, function() v:SetConstantVelocity( 0, 1600 ); end );
							
								Scene.End( "StartJump" );
								
							end );
								
							if( Scene.IsOver( "StartJump" ) ) then
								
								if( GetTicks() > startEscapeAttack ) then
									
									Scene.Once( "BattleEnd", function()
									
									if( v:GetPos().y < 0 ) then
									
										enemyDisappeared = true;
										
										local pos = Engine.GetBattlePlayer( 0 ):GetPos();
										
										v:SetPos( pos.x + 20, 0 );
										v:SetConstantVelocity( 0, 0 );
										
										timer.Simple( 19000, function() Level.BackFromIntroBattle = true; Engine.EndBattle( true ); end );
										
										timer.Simple( 22000, function()
										
											LuaC.FreeText( "Part I", "Uni14", 340, 270, 5000, 15, 0, 0, 0 );
											timer.Simple( 1200, function() LuaC.FreeText( "Goblin.", "Uni26Bold", 340, 280, 3800, 150, 0, 0, 0 ); end );
											
											
										end );
									
										SetBattleDrawFunc( function()
			
											local bottom = v:GetPos().y + v:GetSize().h;
											
											if( bottom > pos.y ) then
												
												Draw.SetColor255( battleRed, 0, 0, 255 );
												Draw.Rect( 0, 120, 800, 360 );
												
												Scene.Do( "Title", function() 
												
												if( startBattleFade < 0 ) then
													
													startBattleFade = GetTicks();
													
												end
												
												Scene.DoAt( "Title", 5500, function()
													
													Draw.SetColor255( 255, 255, 255, 255 );
													Draw.Text( "Uni26Bold", "The Strongest Kid", 250, 250 );
													
													Draw.Rect( 403 - gameTitleUnderline, 282, gameTitleUnderline * 2, 2 );
													
												end );
												
												Scene.DoAt( "Title", 6500, function() 
												
													gameTitleUnderline = gameTitleUnderline + 60 * GetDPS();
													
													if( gameTitleUnderline > 150 ) then
														
														gameTitleUnderline = 150;
														
													end
													
												end );
											
												Scene.DoAt( "Title", 7000, function()
												
													battleRed = battleRed - 50 * GetDPS();
													
													if( battleRed < 0 ) then
													
														battleRed = 0;
														
													end
													
												end );
												
											
												Scene.DoAt( "Title", 15000, function()
												
													Scene.End( "Title" );
													
												end );												
												
												end );
												
											end
			
										end );
										
									
									end
									
									Scene.End( "EndBattle" );
									
									end );
									
								end
								
								
							end
							
						end
				
					end
						
				end
				
			
			end, function()
	
				snoit1:Remove( G.State2DRPG );
				snoit2:Remove( G.State2DRPG );
				snoit3:Remove( G.State2DRPG );
			
			 end );
			
			SetGameRule( "CanMove", 1 );
			
		end );
	
end